package com.wjf.resourceservice.service.impl;

import com.wjf.resourceservice.entity.Playlist;
import com.wjf.resourceservice.entity.Music;
import com.wjf.resourceservice.mapper.PlaylistMapper;
import com.wjf.resourceservice.service.PlaylistService;
import com.wjf.common.entity.Result;
import com.wjf.resourceservice.feign.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 歌单服务实现类
 * 处理歌单相关的业务逻辑
 */
@Service
public class PlaylistServiceImpl implements PlaylistService {

    @Autowired
    private PlaylistMapper playlistMapper;

    @Autowired
    private UserClient userClient;

    @Override
    @Transactional
    public boolean createPlaylist(Playlist playlist) {
        // 获取当前用户信息
        Result<?> userResult = userClient.getCurrentUser();
        Object data = userResult.getData();
        if (!(data instanceof Map)) {
            throw new RuntimeException("用户数据格式错误");
        }
        
        @SuppressWarnings("unchecked")
        Map<String, Object> userData = (Map<String, Object>) data;
        playlist.setCreatorId(Long.valueOf(userData.get("id").toString()));
        
        playlist.setCreateTime(new Date());
        playlist.setStatus(1);
        return playlistMapper.createPlaylist(playlist) > 0;
    }

    @Override
    @Transactional
    public boolean addMusicToPlaylist(Long playlistId, Long musicId) {
        // 添加音乐到歌单，记录创建时间
        return playlistMapper.addMusicToPlaylist(playlistId, musicId, new Date()) > 0;
    }

    @Override
    public List<Playlist> getUserPlaylists(Long userId) {
        // 获取用户的所有歌单
        return playlistMapper.getUserPlaylists(userId);
    }

    @Override
    public List<Music> getPlaylistMusics(Long playlistId) {
        // 获取歌单中的所有音乐
        return playlistMapper.getPlaylistMusics(playlistId);
    }
} 